//
//  CollectionViewController.m
//  Reader
//
//  Created by liuchengming on 16/12/20.
//  Copyright © 2016年 1512.7. All rights reserved.
//

#import "CollectionViewController.h"
#import "CollectionViewCell.h"
#import "ReadingViewController.h"


@interface CollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITextViewDelegate>
{
    NSArray<NSURL *> * texts ;
    NSMutableArray * bookName;
}
@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getBookName];
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuseIdentifier];
    UIImage * backgroundimage = [UIImage imageNamed:@"backgroundimage"];
    UIColor * backgroundColor = [UIColor colorWithPatternImage:backgroundimage];
    self.collectionView.backgroundColor = backgroundColor;
    self.navigationItem.title = @"我的小📖屋";
    self.navigationController.toolbarHidden = YES;
    
    
    //菜单    用if  方便折叠
    if (1){
        RNSwipeBar *swipeBar = [[RNSwipeBar alloc] initWithMainView:[self view]];
        [swipeBar setPadding:20.0f];
        [swipeBar setDelegate:self];
        [self setSwipeBar:swipeBar];
        [[self view] addSubview:[self swipeBar]];
        
        RNBarView *barView = [[[NSBundle mainBundle] loadNibNamed:@"RNBarView" owner:self options:nil] lastObject];
        [barView setDelegate:self];
        [swipeBar setBarView:barView];
        
        UIButton * btn = [UIButton new];
        [btn setTitle:@"退出" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:0];
        btn.frame = CGRectMake(220, 25, 80, 44);
        [btn addTarget:self action:@selector(Qxit) forControlEvents:UIControlEventTouchUpInside];
        [barView addSubview:btn];
    }
}

//退出菜单点击事件
- (void) Qxit
{
    exit(0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark <UICollectionView 必须实现的方法>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return bookName.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"style" forIndexPath:indexPath];
    
    //NSLog(@"=%@",bookName[indexPath.row]);
    NSString * imgname = [NSString stringWithFormat:@"%@.jpeg",bookName[indexPath.row]];
    cell.img.image = [UIImage imageNamed:imgname];
    if(!cell.label.text)
    {
        cell.label.text = bookName[indexPath.row];
        cell.img.image = [UIImage imageNamed:@"default_logo"];
    }else
    {
        cell.label.text = nil;
    }
    return cell;
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"现在是点击%@",indexPath);
    //ReadingView * view = [ReadingView new];
    //view.view.backgroundColor = [UIColor blueColor];
    //[view Textdata:indexPath.row];
    
    ReadingViewController * textView = [ReadingViewController new];
    NSString * str = [NSString stringWithFormat:@"%@",bookName[indexPath.row]];
    textView.string = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:str ofType:@".txt"] encoding:NSUTF8StringEncoding error:nil];
    [self presentViewController:textView animated:YES completion:nil];
    //[self presentViewController:view animated:YES completion:^{ }];
}

#pragma mark <Layout>

//cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(75, 100);
}

//整体边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(30, 16, 30, 16);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 30;
}
/*
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
 */

#pragma mark <bookname>
//获取书的name
- (void)getBookName
{
    NSArray <NSURL *> * jpg = [[NSBundle mainBundle] URLsForResourcesWithExtension:@".jpeg" subdirectory:nil];
    NSString * str = [NSString stringWithFormat:@"%@",jpg];
    NSArray * arrs = [str componentsSeparatedByString:@","];
    //NSLog(@"%@",stri);
    bookName = [NSMutableArray new];
    for (NSString * line in arrs)
    {
        NSArray * arr = [line componentsSeparatedByString:@".jpeg"];
        NSString * str = [NSString stringWithFormat:@"%@",arr.firstObject];
        arr = [str componentsSeparatedByString:@" "];
        str = [NSString stringWithFormat:@"%@",arr];
        arr = [str componentsSeparatedByString:@"\""];
        str = [NSString stringWithFormat:@"%@",arr[10]];
        str = [str stringByRemovingPercentEncoding];
        [bookName addObject:str];
        //NSLog(@"%lu",(unsigned long)bookName.count);
        NSLog(@"bookName====%@",str);
    }
}

#pragma mark <Menu>
//菜单必须实现的方法
- (void)mainButtonWasPressed:(id)sender
{
    [self.swipeBar toggle];
}

@end
